package service;

import DAO.FoundRecordDAO;
import entity.FoundRecord;
import proxy.FoundRecordDaoProxyFactory;

import java.sql.SQLException;


public class FoundRecordService {
    private FoundRecordDAO foundRecordDao;
    private String currentLoginName;

    public FoundRecordService(String loginName) {
        this.currentLoginName = loginName;
        FoundRecordDAO originalDao = new FoundRecordDAO();
        FoundRecordDaoProxyFactory proxyFactory = new FoundRecordDaoProxyFactory();
        proxyFactory.setTarget(originalDao);
        proxyFactory.setCurrentLoningName(loginName);
        this.foundRecordDao = proxyFactory.createProxy();
    }

    public boolean addFoundRecord(FoundRecord record) throws SQLException {
        if (record == null) {
            return false;
        }
        try {
            foundRecordDao.addFoundRecord(record);
            return true;
        } catch (SQLException e) {
            throw e;
        }
    }

    public boolean updateFoundRecord(FoundRecord record) throws SQLException {
        if (record == null || record.getId() == null) {
            return false;
        }
        try {
            foundRecordDao.updateFoundRecord(record);
            return true;
        } catch (SQLException e) {
            throw e;
        }
    }

    public boolean deleteFoundRecord(FoundRecord record) throws SQLException {
        if (record == null || record.getId() == null) {
            return false;
        }
        try {
            foundRecordDao.deleteFoundRecord(record);
            return true;
        } catch (SQLException e) {
            throw e;
        }
    }

    public void selectFoundRecord() throws Exception {
        foundRecordDao.selectFoundRecord();
    }

    public boolean existsRecord(FoundRecord record) {
        try {
            return FoundRecordDAO.existsData(record);
        } catch (Exception e) {
            System.err.println("检查记录存在性时出错: " + e.getMessage());
            return false;
        }
    }
    }



